package org.ws.task.builder;

import org.ws.task.item.ExecuteContext;
import org.ws.task.item.ItemLeaderCommit;
import org.ws.task.item.ItemWorkerProcessor;
import org.ws.task.item.PushTask;
import org.ws.task.item.TaskInstance;
import org.ws.task.item.support.SimplePushTask;
import org.ws.task.model.TaskConfig;
import org.ws.task.repository.TaskRepository;

public final class PushTaskBuilder<T> {

    private PushTask<T> pushTask;
    private TaskConfig taskConfig;
    private ItemLeaderCommit<T> commit;
    private ItemWorkerProcessor<T> processor;

    public PushTaskBuilder(TaskConfig taskConfig){
        this.taskConfig = taskConfig;
        this.taskConfig.setTaskType(TaskConfig.TaskType.PUSH);
        this.pushTask = new SimplePushTask<>(taskConfig.getName());
    }

    public PushTaskBuilder<T> commit(ItemLeaderCommit<T> commit){
        this.commit = commit;
        return this;
    }

    public PushTaskBuilder<T> processor(ItemWorkerProcessor<T> processor){
        this.processor = processor;
        return this;
    }

    public PushTask<T> builder(){
        TaskInstance<T> instance = new TaskInstance<>();
        instance.setTask(this.pushTask);
        instance.setItemWorkerProcessor(this.processor);
        if(this.commit == null){
            this.commit = ItemLeaderCommit.DEFAULT;
        }
        instance.setItemLeaderCommit(this.commit);
        instance.setTaskConfig(this.taskConfig);

        TaskRepository.getInstance().addTaskInstance(instance);
        return this.pushTask;
    }
}
